package com.demo.jpa.service;

import com.demo.jpa.dao.entity.UserEntity;
import com.github.tennaito.rsql.jpa.JpaCriteriaQueryVisitor;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import cz.jirutka.rsql.parser.ast.RSQLVisitor;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import java.util.List;

/**
 * @Author:LiaoYouLe
 * @Date: Created in 2018/7/30
 * @Description
 * @Modifyied By:
 */
@Log4j2
public class RsqlMapToCriteria extends TestCase {
    @Autowired
    private EntityManager entityManager;

    @Test
    public void eq() {
        RSQLVisitor<CriteriaQuery<UserEntity>, EntityManager> userVisitor = new JpaCriteriaQueryVisitor<UserEntity>();
        Node rootNode = new RSQLParser().parse("username==Que*名字");
        CriteriaQuery<UserEntity> query = rootNode.accept(userVisitor, entityManager);
        List<UserEntity> users = entityManager.createQuery(query).getResultList();
        log.info("查询结果：{}",users);
    }
}
